// const uuid = require("uuid")
// console.log(uuid.v4());
const {v4}=require("uuid")
// let md5=require("md5")
const express = require("express")
let router = express.Router()




const query = require("../tools/db")
const {md}=require("../config/config")

// 注册
router.post("/register", async (req, res, next) => {
    // 1、接参post===>req.body
    let { username, password, password2 } = req.body
    // 2、做一系列的判断 
    if (!username) {
        next("请输入账号")
        return
    }
    if (!password) {
        next("请输入密码")
        return
    }
    if (!password2) {
        next("请再次输入密码")
        return
    }

    if (password != password2) {
        next("两次输入的密码不一致")
        return
    }


    // 3、查询数据库 （账号可不可以注册）
    let sql = `select * from e_member where  username="${username}"`
    let [err, results] = await query(sql)

    if (err) {
        next("注册失败")
        return
    }

    if (results.length > 0) {
        next("用户名已被占用")
        return
    }


    // 4、执行注册
    let createdate=+new Date()
    // +new Date()
    // new Date().getTime()
    // Date.now()


    // let sql1 = `insert into 
    //             e_member(mid,username,password,createdate) 
    //             value ("${v4()}","${username}","${ md5(md5(password+"呵呵，猜不到吧")+"$%^(@##@()") }","${createdate}")`

    
    let sql1 = `insert into 
                e_member(mid,username,password,createdate) 
                value ("${v4()}","${username}","${ md(password)}","${createdate}")`



    let [err1, results1] = await query(sql1)

    if (err1) {
        next("注册失败")
        return
    }




    res.send({
        status:200,
        msg:"注册成功",
        id:results1.insertId
    })
})

module.exports = router